package com.wuhenzhizao.sku.utils;

import android.os.Build;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;

import java.util.UUID;

/**
 * Created by wuhenzhizao on 2017/9/20.
 */

public class ViewUtils {

    public static int generateViewId() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return View.generateViewId();
        } else {
            return UUID.randomUUID().hashCode();
        }
    }

    /**
     * 测量view高度
     *
     * @param view
     * @return
     */
    public static int getMeasureHeight(View view) {
        int totalHeight;
        if (view instanceof RecyclerView) {
            totalHeight = ((RecyclerView) view).computeVerticalScrollRange();
        } else {
            int width = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            int height = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            view.measure(width, height);
            totalHeight = view.getMeasuredHeight();
            if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                totalHeight += lp.topMargin + lp.bottomMargin;
                LogUtils.e("lp.topMargin" + lp.topMargin + "lp.bottomMargin" + lp.bottomMargin);
            }

        }
        return totalHeight;
    }
}
